/**
 * 分类配置业务js
 * date: 2010-12-8
 */
COMM.lib.load("dialog");

(function($){
	var CagegoryConfig = (function(){
		
		var containerLeft,containerRight,tbls,parentRow,childRow,categorys,contentLeft,contentRight,tempT,tempP,tempC,optP,optC;
		
		var getCategory = function(){
			$.getJSON("/admin/config/cg_listCategory.do", function(data){
				render(data);
			});
		};
		var render = function(data){
			contentLeft = $("<div></div>");
			contentRight = $("<div></div>");
			categorys = data.categorys;
			for(var i=0;i<categorys.length;i++){
				if(i%2 == 0){
					contentLeft.append(renderTBL(categorys[i]));
				} else {
					contentRight.append(renderTBL(categorys[i]));
				}
			}
			containerLeft.empty().append(contentLeft.children());
			containerRight.empty().append(contentRight.children());
			contentLeft = contentRight = null;
		};
		var renderTBL = function(category){
			tempT = $(tbls);
			tempT.find("tbody").append(renderParent(category));
			for(var j=0; j<category.subCategory.length;j++){
				tempT.find("tbody").append(renderChild(category.subCategory[j]));
			}
			return tempT;
		};
		var renderParent = function(category){
			tempP = $(parentRow.replace("#-parentId-", category.id).replace("#-parentName-", category.name));
			optP = tempP.find(".opt span");
			optP.eq(0).click(function(){
				addSubCategory(category.id);
			});
			optP.eq(1).click(function(){
				editParent(category.id, category.name);
			});
			optP.eq(2).click(function(){
				delParent(category.id);
			});
			return tempP;
		};
		var renderChild = function(childCategory){
			tempC = $(childRow.replace("#-childId-", childCategory.id).replace("#-childName-", childCategory.name));
			optC = tempC.find(".opt span");
			optC.eq(0).click(function(){
				editChild(childCategory.id, childCategory.name);
			});
			optC.eq(1).click(function(){
				delChild(childCategory.id);
			});
			return tempC;			
		};
		var addParent = function(categoryName){
			$.postJson({
				url: "/admin/config/cg_addCategory.do",
				data: {
					categoryName : categoryName
				},
				success: function(data){
					render(data);
				}
			});
		};
		var editParent = function(id, name){
			$.dialog({
				title:'修改分类名称',
			    content: '分类名称：<input type="text" id="editCategoryNameInput" value="'+name+'" class="categoryConfigInput"/>',
			    yesFn : function(){
					var categoryName = jQuery.trim(jQuery("#editCategoryNameInput").val());
					if(categoryName != ''){
						$.postJson({
							url: "/admin/config/cg_editCategory.do",
							data: {
								categoryId : id,
								categoryName : categoryName
							},
							success: function(data){
								render(data);
							}
						});						
					}
				}
			});				
		};
		var delParent = function(id){
			$.dialog.confirm('确定要删除该分类吗？删除该分类将会同时删除其子类！',function(){
				$.getJSON("/admin/config/cg_delCategory.do?categoryId="+id, function(data){
					render(data);
				});					
			},function(){});
		};
		var addSubCategory = function(categoryId){
			$.dialog({
				title:'输入子分类名称',
			    content: '子分类名称：<input type="text" id="subCategoryNameInput" class="categoryConfigInput"/>',
			    yesFn : function(){
					var subCategoryName = jQuery.trim(jQuery("#subCategoryNameInput").val());
					if(subCategoryName != ''){
						$.postJson({
							url: "/admin/config/cg_addSubcategory.do",
							data: {
								categoryId : categoryId,
								categoryName : subCategoryName
							},
							success: function(data){
								render(data);
							}
						});
					}
				}
			});		
		};
		var editChild = function(id, name){
			$.dialog({
				title:'修改分类名称',
			    content: '分类名称：<input type="text" id="editSubcategoryNameInput" value="'+name+'" class="categoryConfigInput"/>',
			    yesFn : function(){
					var categoryName = jQuery.trim(jQuery("#editSubcategoryNameInput").val());
					if(categoryName != ''){
						$.postJson({
							url: "/admin/config/cg_editSubcategory.do",
							data: {
								categoryId : id,
								categoryName : categoryName
							},
							success: function(data){
								render(data);
							}
						});							
					}
				}
			});				
		};
		var delChild = function(id){
			$.dialog.confirm('确定要删除该子类吗？',function(){
				$.getJSON("/admin/config/cg_delSubcategory.do?categoryId="+id, function(data){
					render(data);
				});		
			},function(){});			
		};
		
		return {
			init : function(){
				containerLeft = $("#contentTbls div.left");
				containerRight = $("#contentTbls div.right");
				tbls = '<table class="category-Config-TBl">'+
							'<thead class="gradient">'+
								'<tr>'+
								'	<th class="id">ID</th>'+
								'	<th class="name">名称</th>'+
								'	<th class="opt">操作</th>'+	
								'</tr>'+
							'</thead>'+
							'<tbody></tbody>'+
						'</table>';
				parentRow = '<tr class="parent">'+
							'	<td>#-parentId-</td>'+
							'	<td>#-parentName-</td>'+
							'	<td>'+
							'		<div class="tr opt"><span class="icon icon-add" >&nbsp;</span>'+
							'		<span class="icon icon-edit" >&nbsp;</span>'+
							'		<span class="icon icon-del" >&nbsp;</span></div>'+
							'	</td>'+
							'</tr>';
				childRow = '<tr class="child">'+
							'	<td>#-childId-</td>'+
							'	<td>#-childName-</td>'+
							'	<td>'+
							'		<div class="tr opt"><span class="icon icon-edit">&nbsp;</span>'+
							'		<span class="icon icon-del">&nbsp;</span></div>'+
							'	</td>'+
							'</tr>';
				getCategory();
			},
			addParent : function(categoryName){
				addParent(categoryName);
			}
		};
	})();
	
	$(document).ready(function(){
		
		CagegoryConfig.init();
		$("#addCategoryBtn").click(function(){
			$.dialog({
				title:'输入分类名称',
			    content: '分类名称：<input type="text" id="categoryNameInput" class="categoryConfigInput"/>',
			    yesFn : function(){
					if(jQuery.trim(jQuery("#categoryNameInput").val()) != ''){
						CagegoryConfig.addParent(jQuery("#categoryNameInput").val());
					}
				}
			});
		});
	});
})(jQuery);